﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Company.Core.Helper;
using Company.Hardware;
using Company.Hardware.Config;
using Company.Hardware.Ice_ipc;
using FastReport.Utils;
using MahApps.Metro.Controls;
using MaterialDesignThemes.Wpf;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using System.Windows.Input;
using 无人值守地磅称重系统.Interfaces;
using 无人值守地磅称重系统.Services;

namespace 无人值守地磅称重系统.ViewModels
{
    public class TestViewModel : ObservableObject
    {

        public ICommand TriggerCameraCommand { get; }
        public ICommand ShowLargeCommand { get; }

        public ICameraIce CameraIce { get; }
        public ISession Session { get; }
        public ICamera Camera { get; }
        public IScale Scale { get; }
        public IHardwareManager HardwareManager { get; }
        public IConfigService ConfigService { get;  }

        public TestViewModel(
            ICameraIce cameraIce,
            ISession session,
            ICamera camera,
            IScale scale,
            IHardwareManager hardwareManager,
            IConfigService configService) 
        {
            CameraIce = cameraIce;
            Session = session;
            Camera = camera;
            Scale = scale;
            HardwareManager = hardwareManager;
            ConfigService = configService;

            TriggerCameraCommand = new RelayCommand<String>(TriggerCamera);
            ShowLargeCommand = new RelayCommand(ShowLarge);
        }

        private void ShowLarge()
        {

            //Window newWindow = new Window { Title = "视频预览" };
            Window newWindow = new MetroWindow();


            //创建要添加到窗口中的元素
            StackPanel stackPanel = new StackPanel();
            // 添加WindowsFormsHost控件
            WindowsFormsHost windowsFormsHost = new WindowsFormsHost();
            // 创建WinForms的PictureBox实例
            PictureBox pb = new PictureBox();
            pb.Width = 800;
            pb.Height = 800;

            // 将PictureBox添加到WindowsFormsHost控件中
            windowsFormsHost.Child = pb;

            stackPanel.Children.Add(windowsFormsHost);
            // 将StackPanel添加到新窗口的Content属性
            newWindow.Content = stackPanel;

            //// 显示摄像头预览
            if (CameraIce.Initialized)
            {
                HardwareConfig config = ConfigService.HardwareConfig;

                CameraIce.OpenPreview(config.IceIPAddress, 1, 1, (uint)pb.Handle, null, new IntPtr());
            }

            //// 显示窗体
            newWindow.ShowDialog();


        }

        /// <summary>
        /// 触发车牌识别
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void TriggerCamera(string carName)
        {
            CameraIce.Trigger(carName);
        }
    }
}
